<?php
// Open Header, include functions and get the showid and cached xml
include 'functions.php';
loadheader();
$loc = $_GET['loc'];

$cache_time = 3600*24; // 24 hours

$cache_file = './cache/current.xml';
$timedif = @(time() - filemtime($cache_file));

if (file_exists($cache_file) && $timedif < $cache_time) {
    $string = file_get_contents($cache_file);
} else {
    $string = file_get_contents('http://services.tvrage.com/feeds/currentshows.php');
    if (($string) && ($f = @fopen($cache_file, 'w'))) {
        fwrite ($f, $string, strlen($string));
        fclose($f);
    }
}
// Cached the file, now lets use it.
if (!file_exists($cache_file)) { // Does it exist? If it doesn't there's a problem so lets report it.
 echo "<center><h2>Cache file issue: Cannot open cached XML poss connection issue?</h2></center>";
 break;
}

$xml = simplexml_load_file($cache_file);

echo "<table border=0 width=600px; cellpadding=2>\n";
foreach($xml->country as $country){
 	echo "<tr><th colspan=\"2\"><b>Country List of: ".$country['name']."</b></th></tr>\n";
	echo "<tr><th width=30px;>Show:</th><th width=70px;>TVRage Link:</th></tr>\n";
	echo "<tr></tr>\n";
 	foreach($country->show as $node){
 		echo "<tr><td><a href=\"eplist.php?sid=".$node->showid."\">".make_safe($node->showname)."</a></td>\n";
 		echo "<td><a href=\"".$node->showlink."\">Lookup @ TV Rage.com</a></td>\n";
 	  
 	}
}echo "</table>\n";
echo "<p><div align=center>Page generated using a cached feed generated on: ".date ("F d Y H:i:s.", filemtime($cache_file))."</div></p>\n";


loadfooter();	

?>